package com.example._21090102110liuyang;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service
public class StudentService {
    private final List<Student> students = new ArrayList<>();
    public StudentService() {
        // 初始化一些学生数据
        students.add(new Student(1L, "Alice", "Smith", "alice@example.com", 20));
        students.add(new Student(2L, "Bob", "Brown", "bob@example.com", 22));
    }
    public List<Student>
    findAllStudents() {
        return students;
    }
    public Student createStudent(Student student) {
        student.setId((long) (students.size() + 1));
        students.add(student);
        return student;
    }
    public Optional<Student> findStudentById(Long id) {
        return students.stream().filter(student -> student.getId().equals(id)).findFirst();
    }
    public Student updateStudent(Long id, Student studentDetails) {
        Student student = findStudentById(id).orElse(null);
        if (student != null) {
            student.setFirstName(studentDetails.getFirstName());
            student.setLastName(studentDetails.getLastName());
            student.setEmail(studentDetails.getEmail());
            student.setAge(studentDetails.getAge());
            return student;
        }
        return null;
    }
    public void deleteStudent(Long id) {
        students.removeIf(student -> student.getId().equals(id));
    }
}